package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Customer;

/**
 *
 * @author Wei Liu
 */
public class CustomerDAO extends BaseEntityDAO
{

    public CustomerDAO()
    {
        // initialization
    }

    /**
     * Insert customer into database
     * @param customer the new customer entity to be inserted
     * @return newly created customer number or a 0 on error
     */
    public int insertCustomer(Customer customer)
    {
        return save(customer);
    }

    /**
     * Delete customer that matches the supplied criteria
     * @param customer the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteCustomer(Customer customer)
    {
        return delete(customer);
    }

    /**
     * Update the customer using data from the customer Data Object
     * @param customer the customer Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateCustomer(Customer customer)
    {
        if (customer.getCustomerID() == 0)
        {
            return false;
        }

        if (update(customer))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the customer that matches the supplied criteria
     * @param customer the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Customer findCustomer(Customer customer)
    {
        String sql = null;

        if(customer.getCustomerID()!=0)
            sql = buildQuerySQL(customer);
        else
            return null;

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Customer) this.getObject(crs, customer);
    }

    /**
     * Get all customers that match the supplied criteria
     * @param customer supplied criteria
     * @return a RowSet
     */
    public RowSet getCustomersRS(Customer customer)
    {
        String sql = buildQuerySQL(customer);
        return query(sql);
    }

    /**
     * Get all customers that match the supplied criteria
     * @param customer supplied criteria
     * @return a collection of Customer Data Objects
     */
    public Collection<Customer> getCustomersDO(Customer customer)
    {
        String sql = buildQuerySQL(customer);

        RowSet crs = query(sql);
        Collection<Customer> customers = new ArrayList<Customer>();
        try
        {
            while (crs.next())
            {
                Customer customerDO = (Customer) this.getObject(crs, customer);
                Customer item = new Customer();
                item.setAddressLine1(customerDO.getAddressLine1());
                item.setAddressLine2(customerDO.getAddressLine2());
                item.setCity(customerDO.getCity());
                item.setCustomerID(customerDO.getCustomerID());
                item.setEmailAddress(customerDO.getEmailAddress());
                item.setFirstName(customerDO.getFirstName());
                item.setLastName(customerDO.getLastName());
                item.setPhoneNo(item.getLastName());
                item.setPostcode(customerDO.getPostcode());
                item.setProvince(customerDO.getProvince());
                customers.add(item);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return customers;
    }
}
